<template>
  <div class="Sku">
    <el-table :data="tableData" border>
      <el-table-column label="序号" width="80" align="center">
        <template v-slot="{$index}">
          {{$index+1}}
        </template>
      </el-table-column>
      <el-table-column label="名称" prop="skuName"></el-table-column>
      <el-table-column label="描述" prop="skuSaleAttrValueList"></el-table-column>
      <el-table-column label="默认图片">
        <template v-slot="{row}">
          <img :src="row.skuDefaultImg" style="width:50px;height:50px">
        </template>
      </el-table-column>
      <el-table-column label="重量(KG)" prop="weight" width="100"></el-table-column>
      <el-table-column label="价格(元)" prop="price" width="100"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="{row}">
          <el-button type="success" icon="el-icon-sort-down" size="mini" v-if="row.isSale" @click="hanleDown(row)"></el-button>
          <el-button type="success" icon="el-icon-sort-up" size="mini" v-else @click="handleUp(row)"></el-button>
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="handleEdit(row)"></el-button>
          <el-button type="info" icon="el-icon-info" size="mini" @click="handleCheck(row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination" style="margin-top: 20px; text-align: center">
      <el-pagination
        :total="pagination.total"
        :page-size="pagination.pageSize"
        :page-sizes="[1, 3, 5, 10]"
        :current-page="pagination.currentPage"
        layout="prev,pager,next,jumper,->,sizes,total"
        @current-change="handleCurChange"
        @size-change="handleSizeChange"
      />
    </div>
    <Drawer v-model="drawerShow" :skuInfo="skuInfo"></Drawer>
  </div>
</template>

<script>
import { getSkuList,onSale,cancelSale,getSkuById } from "@/api/product/sku";
import Drawer from "./Drawer.vue";
import pagination from '@/mixins/pagination'
export default {
  name:'Sku',
  mixins: [pagination],
  components:{
    Drawer
  },
  data(){
    return {
      tableData:[],
      skuInfo:{},
      drawerShow: false,
    }
  },
  created(){
    this.getData()
  },
  methods:{
    async getData(){
      const {currentPage,pageSize} = this.pagination
      const res =await getSkuList(currentPage, pageSize)
      this.tableData = res.records
      this.pagination.total = res.total
    },
    // 上架
    async handleUp(row){
      await onSale(row.id)
      row.isSale = 1
      this.$message.success('上架成功')
    },
    // 下架
    async hanleDown(row){
      await cancelSale(row.id)
      row.isSale = 0
      this.$message.success('下架成功')
    },
    // 编辑
    handleEdit(row){
      this.$message.info('正在开发中')
    },
    // 查看sku详情
    async handleCheck(row){
      const res = await getSkuById(row.id)
      this.skuInfo = res
      this.drawerShow = true
    }
  }
}
</script>

<style scoped lang="scss">
  ::v-deep .el-carousel__button{
    background-color: #09f;
    width: 10px;
    height: 10px;
    border-radius: 50%;
  }
</style>